#include "spectrumanalysis.h"
#include <cmath>
#include <stdexcept>
/*
// 生成时域信号离散序列
vector<double> generateWaveNormal(const map<string, double>& params) {
    // 从 map 中提取参数
    int N = static_cast<int>(params.at("N")); // 采样点数
    double Fs = params.at("Fs"); // 采样频率
    double f = params.at("f");   // 信号频率

    vector<double> signal(N);
    double T = 1.0 / Fs; // 采样间隔

    for (int i = 0; i < N; ++i) {
        double t = i * T;
        signal[i] = sin(2 * M_PI * f * t); // 生成正弦波
    }
    return signal;
}

// 对时域信号进行傅里叶变换
fftw_complex* FFT(int N, const vector<double>& x) {
    // 分配输入和输出数组
    fftw_complex* in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);
    fftw_complex* out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);

    // 创建 FFT 计划
    fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);

    // 填充输入数据
    for (int i = 0; i < N; ++i) {
        in[i][0] = x[i]; // 实部
        in[i][1] = 0.0;  // 虚部
    }

    // 执行 FFT
    fftw_execute(plan);

    // 释放资源
    fftw_destroy_plan(plan);
    fftw_free(in);

    return out;
}

// 计算幅频图
vector<double> Amplitude(int N, double Fs, const fftw_complex* out) {
    vector<double> amplitude(N / 2);
    for (int i = 0; i < N / 2; ++i) {
        double real = out[i][0];
        double imag = out[i][1];
        amplitude[i] = sqrt(real * real + imag * imag); // 计算幅值
    }
    return amplitude;
}

// 计算功率谱
vector<double> PowerSpectrum(int N, double Fs, const fftw_complex* out) {
    vector<double> powerSpectrum(N / 2);
    for (int i = 0; i < N / 2; ++i) {
        double real = out[i][0];
        double imag = out[i][1];
        powerSpectrum[i] = (real * real + imag * imag) / N; // 计算功率谱
    }
    return powerSpectrum;
}

// 计算功率谱密度
vector<double> PowerSpectrumDensity(int N, double Fs, const fftw_complex* out) {
    vector<double> powerSpectrumDensity(N / 2);
    double df = Fs / N; // 频率分辨率

    for (int i = 0; i < N / 2; ++i) {
        double real = out[i][0];
        double imag = out[i][1];
        powerSpectrumDensity[i] = (real * real + imag * imag) / (df * N); // 计算功率谱密度
    }
    return powerSpectrumDensity;
}*/
